home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / comm / misc / mirrorman_1_10b1.lha / MirrorManager-1.10b1 / rexx / MakeTree.mm < prev    next >
Text File  |  1994-06-24  |  9KB  |  356 lines

  1. /*rx
  2.     $VER: $Id: MakeTree.mm,v 1.10 1994/06/20 01:08:07 tf Exp $
  3.  
  4.     Create all directories listed in a tree file relative to a given directory.
  5.     The directory structure and a comment for each directory will be taken
  6.     from a TREE file.
  7.  
  8.     This ARexx script needs the AmigaDOS commands "MakeDir", "Filenote" and
  9.     "Delete" available in your path.
  10.  
  11.     Initial revision by Tobias Ferber, 21-Feb-94
  12. */
  13.  
  14. options results
  15. options failat 10
  16.  
  17. /* default values */
  18.  
  19. treefile = ""
  20. destpath = ""   /* parent dir of those listed in the treefile */
  21. tempfile = "T:MakeTreeTemp." || pragma('Id')
  22. template = "FROM/K/A,TO/K/A,NOCREATE/S,AUTO/S"
  23. cliopts  = ""
  24.  
  25. dg       = 0  /* gauge increment */
  26. gstepN   = 0
  27.  
  28. ESC      = '1b'x
  29.  
  30. signal on HALT
  31. signal on BREAK_C
  32. signal on BREAK_D
  33.  
  34.  
  35. /* parse args */
  36.  
  37. do ac=1 while ac <= arg()
  38.   av= arg(ac)
  39.   select
  40.     when upper(av) = "FROM" then do
  41.       if ac < arg() then do
  42.         ac= ac+1
  43.         treefile= arg(ac)
  44.         end
  45.       else exit bad_args('Missing Aminet TREE path/filename after' ESC'bFROM'ESC'n keyword.')
  46.       end /* FROM */
  47.  
  48.     when upper(av) = "TO" then do
  49.       if ac < arg() then do
  50.         ac= ac+1
  51.         destpath= arg(ac)
  52.         if words(destpath) < 1 then destpath= pragma('D')
  53.         end
  54.       else exit bad_args('Missing destination pathname after' ESC'bTO'ESC'n keyword.')
  55.       end /* TO */
  56.  
  57.     when upper(av) = "AUTO"     then cliopts= cliopts || 'a'
  58.     when upper(av) = "NOCREATE" then cliopts= cliopts || 'n'
  59.  
  60.     otherwise exit bad_args('Unknown keyword:' ESC'b' || av || ESC'n')
  61.  
  62.     end /* select */
  63.  
  64.   end /* do */
  65.  
  66. call pragma('W','N')
  67.  
  68.  
  69. /* try to get missing tree filename */
  70.  
  71. if words(treefile) < 1 then do
  72.   cwd= strip(pragma('D'),'B','"')
  73.   REQUESTFILE DRAWER '"'cwd'"' TITLE '"Select a TREE file..."' NOICONS
  74.   if (rc=0) & (words(result) > 0) & (result ~= 'RESULT') then treefile= result
  75.   end
  76.  
  77. if words(treefile) < 1 then
  78.   exit bad_args("Not enough arguments for MakeTree...*nExiting...")
  79.  
  80. if ~exists(treefile) then do
  81.   REQUESTCHOICE TITLE   '"MakeTree Request"',
  82.                 BODY    '"MakeTree failed to locate your tree file*n*n' ||,
  83.                         ESC'c'ESC'b' || treefile || ESC'n'ESC'l'        || '"',
  84.                 GADGETS '"Exit"'
  85.   exit 10
  86.   end
  87.  
  88. /* get missing destination pathname */
  89.  
  90. if words(destpath) < 1 then do
  91.   cwd= strip(pragma('D'),'B','"')
  92.   REQUESTFILE DRAWER '"'cwd'"' TITLE '"Select a destination path..."' DRAWERSONLY NOICONS SAVEMODE
  93.   if (rc=0) & (words(result) > 0) & (result ~= 'RESULT') then destpath= result
  94.   end
  95.  
  96. if words(destpath) < 1 then
  97.   exit bad_args("Not enough arguments for MakeTree...*nExiting...")
  98.  
  99. if ~exists(destpath) & canexist(destpath) then do
  100.   REQUESTCHOICE TITLE   '"MakeTree Request"',
  101.                 BODY    '"Destination path*n*n'                      ||,
  102.                         ESC'c'ESC'b' || destpath || ESC'n'ESC'l*n*n' ||,
  103.                         'does not exist.  Shall I create it?'        || '"',
  104.                 GADGETS '"_Yes|_No"'
  105.  
  106.   if result = '1' then call makepath(destpath)
  107.   end
  108.  
  109. if ~exists(destpath) then do
  110.   REQUESTCHOICE TITLE   '"MakeTree Request"',
  111.                 BODY    '"Failed to locate your destination path*n*n' ||,
  112.                         ESC'c'ESC'b' || destpath || ESC'n'ESC'l'      || '"',
  113.                 GADGETS '"Exit"'
  114.   exit 10
  115.   end
  116.  
  117.  
  118. CALL init_gauge(treefile,2)
  119.  
  120.  
  121. signal on ERROR
  122. signal on IOERR
  123.  
  124. signal on FAILURE
  125. /*signal on NOVALUE*/
  126. signal on SYNTAX
  127.  
  128.  
  129. /* open the TREE file */
  130.  
  131. if ~open('fp',treefile,'R') then do
  132.   REQUESTCHOICE TITLE   '"MakeTree Request"',
  133.                 BODY    '"Could not open your tree file*n*n'     ||,
  134.                         ESC'c'ESC'b' || treefile || ESC'n'ESC'l' || '"',
  135.                 GADGETS '"Exit"'
  136.   exit 10
  137.   end
  138.  
  139. do until eof('fp')
  140.  
  141.   line= strip( readln('fp') )
  142.  
  143.   if ( words(line) > 0 ) & ( left(line,1) ~= '#' ) & ( left(line,1) ~= ';') then do
  144.     pathname = tackon( destpath, word(line,1) )
  145.     comment  = strip( delword(line,1,1) )
  146.  
  147.     if ~exists(pathname) & (pos('n',cliopts) < 1) then do
  148.       if canexist(pathname) then do
  149.         call makepath(pathname)
  150.         str= "[created]"
  151.         end
  152.       else do
  153.         REQUESTCHOICE TITLE   '"MakeTree Warning"',
  154.                       BODY    '"Illegal pathname:*n*n'ESC'c'ESC'b' || pathname || ESC'n'ESC'l"',
  155.                       GADGETS '"Ignore"'
  156.         MESSAGE '"Illegal pathname:' pathname '... ignored"'
  157.         str= "[illegal]"
  158.         end
  159.       end
  160.     else str= "[exists]"
  161.  
  162.     CALL step_gauge(1)
  163.  
  164.     if exists(pathname) then do
  165.       if words(comment) > 0 then do
  166.         address command 'Filenote FILE' '"'pathname'"' 'COMMENT' transquote(comment)
  167.         end
  168.  
  169.       MESSAGE transquote( left(pathname,30) || left(comment,40) || str )
  170.       end
  171.  
  172.     CALL step_gauge(1)
  173.  
  174.     end /* non-empty line */
  175.  
  176.   end /* do */
  177.  
  178. call step_gauge(100)
  179. call close('fp')
  180. IF POS('a',cliopts) > 0 THEN MESSAGE CLOSE
  181. exit
  182.  
  183.  
  184. /**/
  185.  
  186. bad_args: PROCEDURE EXPOSE template ESC
  187.   PARSE ARG msg
  188.  
  189.   REQUESTCHOICE TITLE   '"MakeTree Request"',
  190.                 BODY    '"' || msg || '*n*n'                     ||,
  191.                         'MakeTree args template:*n*n'            ||,
  192.                         ESC'c'ESC'b' || template || ESC'n'ESC'l' || '"',
  193.                 GADGETS '"Okay"'
  194.   RETURN 0
  195.  
  196.  
  197. /*@*/
  198.  
  199.  
  200. /* translate '"' into '*"' and '*' into '**' */
  201.  
  202. transquote: procedure
  203.   parse arg s
  204.   t= s
  205.   q= max( lastpos('*',s), lastpos('"',s) )
  206.  
  207.   do while q > 0
  208.     t= insert('*',t,q-1,1)
  209.     s= left(s,q-1)
  210.     q= max( lastpos('*',s), lastpos('"',s) )
  211.     end
  212.  
  213.   return '"' || t || '"'
  214.  
  215.  
  216. /* return the non-file part of a pathname */
  217.  
  218. pathonly: procedure
  219.   parse arg path
  220.   if (words(path) > 0) & (right(path,1) ~= ':') then do
  221.     if right(path,1) = '/' then path= left(path,length(path)-1)
  222.     if lastpos('/',path) > lastpos(':',path) then path= left(path,lastpos('/',path)-1)
  223.                                              else path= left(path,lastpos(':',path))
  224.     end
  225.   return path
  226.  
  227.  
  228. /* concatenate the filename to the pathname and return the resulting string */
  229.  
  230. tackon: procedure
  231.   parse arg path,file
  232.   do while left(file,1) = '/'
  233.     file= substr(file,2)
  234.     path= pathonly(path)
  235.     end
  236.   if (words(path) > 0) & (right(path,1) ~= '/') & (right(path,1) ~= ':') then path= path || '/'
  237.   if (right(file,1) = '/') then file= left(file,length(file)-1)
  238.   return path || file
  239.  
  240.  
  241. /* create all non-existant directories in a path */
  242.  
  243. makepath: procedure
  244.   parse arg path
  245.   if right(path,1) = '/' then path= left(path,length(path)-1)
  246.   if ~exists(path) then do
  247.     call makepath( pathonly(path) )
  248.     address command 'MakeDir NAME "'path'"'
  249.     end
  250.   return 0
  251.  
  252.  
  253. /*
  254.  * return   1  if the device or volume name in given pathname exists
  255.  *             or if no device or volume was present (current device)
  256.  *          0  if the device or volume name does not exist
  257.  */
  258.  
  259. canexist: procedure
  260.   parse upper arg path
  261.   if pos(':',path) < 1 then return 1 /* current device */
  262.   call pragma('W','N')
  263.   return exists( left(path,lastpos(':',path)) )
  264.  
  265.  
  266. /* stretch the blue completion bar */
  267.  
  268. step_gauge: PROCEDURE EXPOSE dg gstepN
  269.   ARG increment
  270.   gstepN= gstepN + 1
  271.   c= MIN(TRUNC(gstepN * increment * dg),100)
  272.   COMPLETE c
  273.   IF c >= 100 THEN WORKING '"done."'
  274.   RETURN 0
  275.  
  276.  
  277. /* initialize the gauge increment by counting the #of steps to be performed */
  278.  
  279. init_gauge: PROCEDURE EXPOSE dg gstepN
  280.   PARSE ARG fname,steps_per_entry
  281.  
  282.   dg = 0         /* gauge increment */
  283.   gstepN = 0     /* #of performed steps */
  284.  
  285.   IF OPEN('fp',fname,'R') THEN DO
  286.     numentries= 0
  287.     DO UNTIL EOF('fp')
  288.       IF WORDS(READLN('fp')) > 0 THEN
  289.         numentries= numentries+1
  290.       END
  291.     WORKING '"Processing' numentries 'entries..."'
  292.     dg = 100 / (numentries * steps_per_entry)
  293.     CALL SEEK('fp',0,'B')
  294.     CALL CLOSE('fp')
  295.     END
  296.  
  297.   MESSAGE CLEAR; MESSAGE OPEN
  298.   COMPLETE 0
  299.   RETURN 0
  300.  
  301.  
  302. /* error/break handling */
  303.  
  304. IOERR:
  305. ERROR:
  306.   err= rc
  307.   ESC = '1b'x
  308.  
  309.   signal off ERROR
  310.   signal off IOERR
  311.  
  312.   WORKING '"I/O problem trapped... Execution halted."'
  313.   MESSAGE '"I/O problem trapped... Execution halted."'
  314.  
  315.   REQUESTCHOICE TITLE   '"MakeTree Error Trap' err'"',
  316.                 BODY    '"There was a problem with external I/O in line' sigl '...*n' ||,
  317.                         ESC'c'ESC'b' || ERRORTEXT(err) || ESC'n'ESC'l'                || '"',
  318.                 GADGETS '"I''ll better exit"'
  319.   exit
  320.  
  321.  
  322. FAILURE:
  323. NOVALUE:
  324. SYNTAX:
  325.   err= rc
  326.   ESC = '1b'x
  327.  
  328.   signal off FAILURE
  329.   signal off NOVALUE
  330.   signal off SYNTAX
  331.  
  332.   WORKING '"Internal problem trapped... Execution halted."'
  333.   MESSAGE '"Internal problem trapped... Execution halted."'
  334.  
  335.   REQUESTCHOICE TITLE   '"MakeTree Internal Error' err'"',
  336.                 BODY    '"MakeTree seems to have an internal problem in line' sigl '...*n' ||,
  337.                         ESC'c'ESC'b' || ERRORTEXT(err) || ESC'n'ESC'l'                     || '"',
  338.                 GADGETS '"I''ll better exit"'
  339.   exit
  340.  
  341.  
  342. HALT:
  343. BREAK_C:
  344. BREAK_D:
  345.   signal off HALT
  346.   signal off BREAK_C
  347.   signal off BREAK_D
  348.  
  349.   WORKING '"Break signal trapped... Execution halted."'
  350.   MESSAGE '"Break signal trapped... Execution halted."'
  351.  
  352.   REQUESTCHOICE TITLE   '"MakeTree Break Trap"',
  353.                 BODY    '"Script execution halted."',
  354.                 GADGETS '"Stop"'
  355.   exit
  356.